package org.jha.video.hhnet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jha.base.CommonTools;
import org.jha.video.hhnet.HHNet.ChannelStreamCallback;
import org.jha.video.hhnet.HHNet.ENCODE_VIDEO_TYPE;
import org.jha.video.hhnet.HHNet.HHOPEN_CHANNEL_INFO_EX;
import org.jha.video.hhnet.HHNet.HHOPEN_PICTURE_INFO;
import org.jha.video.hhnet.HHNet.PictureCallback;

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.IntByReference;

public class HHNETDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {

		Panel p = new Panel();
		JFrame f = new JFrame();
		p.setPreferredSize(new Dimension(400, 300));
		// p.setBackground(Color.RED);

		final Panel pvideo = new Panel();
		pvideo.setPreferredSize(new Dimension(400, 300));
		pvideo.setBackground(Color.GREEN);
		p.add(pvideo);
		f.setLayout(new BorderLayout());
		f.add(p, BorderLayout.CENTER);
		JPanel cmdPanel = new JPanel();
		cmdPanel.setLayout(new FlowLayout());
		final JButton start = new JButton("start");
		final JButton stop = new JButton("stop");
		final JButton capture = new JButton("capture");
		cmdPanel.add(start);
		cmdPanel.add(stop);
		cmdPanel.add(capture);
		start.setEnabled(true);
		stop.setEnabled(false);
		f.add(cmdPanel, BorderLayout.SOUTH);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.pack();
		f.setVisible(true);
		final HWND parentWnd = CommonTools.createHWNDByComponent(p);

		final HWND videoWnd = CommonTools.createHWNDByComponent(pvideo);

		final int nPort = 1;

		// IntByReference hServer = new IntByReference();
		int init = HHNet.INSTANCE.HHNET_Startup(parentWnd, 0);
		System.out.println("init=" + init);
		HH5PlayerSDK.INSTANCE.HH5PLAYER_SetDecoderQulity(0);
		HH5PlayerSDK.INSTANCE.HH5PLAYER_InitPlayer2(nPort, videoWnd);
		HH5PlayerSDK.INSTANCE.HH5PLAYER_OpenStream(nPort);

		int frameSize = HH5PlayerSDK.INSTANCE.HH5PLAYER_GetStreamFrameSize(1);

		System.out.println(frameSize);

		final String pServerIP = "192.168.55.160";
		final NativeLong wServerPort = new NativeLong(5000);
		final String pDeviceName = "DVS707601";
		final String pUserName = "admin";
		final String pUserPassword = "admin";
		final int dwClientID = 0;
		final int nOpenChannel = 0;
		final int protocolType = 0;
		// int res = HHNet.INSTANCE.HHNET_LogonServer(pServerIP, wServerPort,
		// pDeviceName, pUserName, pUserPassword, 0, hServer);
		//
		// if (res != 0) {
		// System.out.println("logon=" + res);
		// return;
		// }
		// System.out.println("hServer=" + hServer.getValue());
		// HH_SERVER_INFO serverInfo = new HH_SERVER_INFO();
		// HHNet.INSTANCE.HHNET_ReadServerInfo(hServer, serverInfo);
		// System.out.println("nServerChannelNum:"+serverInfo.logonStatus);
		final IntByReference hOpenChannel = new IntByReference();
		final HHOPEN_CHANNEL_INFO_EX pOpenInfo = new HHOPEN_CHANNEL_INFO_EX();
		pOpenInfo.dwClientID = dwClientID;
		pOpenInfo.nOpenChannel = nOpenChannel;
		pOpenInfo.protocolType = protocolType;
		pOpenInfo.pCallbackContext = videoWnd;

		pOpenInfo.funcStreamCallback = new ChannelStreamCallback() {

			@Override
			public void invoke(int hOpenChannel, Pointer pStreamData,
					long dwClientID, HWND pContext,
					ENCODE_VIDEO_TYPE encodeVideoType, Pointer pAVInfo) {
				// print(pStreamData.getByteArray(0, 1024*100));
				// System.out.println("pStreamData=" +
				// print(pStreamData.getByteArray(0, 1024*10)));
				long size = 1024 * 128;
				pStreamData.getByteArray(0, (int) size);
				// System.out.println(encodeVideoType.type);
				HH5PlayerSDK.INSTANCE.HH5PLAYER_PutDecStreamData(nPort,
						pStreamData, size, encodeVideoType.type);
				// HHNet.INSTANCE.HHNET_ViewPushSend(0, pStreamData, 1024*10,
				// 16);
			}
		};

		start.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// HHNet.INSTANCE.HHNET_CloseChannel(hOpenChannel);
				HHNet.INSTANCE.HHNET_CloseChannel(hOpenChannel.getValue());
				int openchannel = HHNet.INSTANCE.HHNET_OpenChannel(pServerIP,
						wServerPort, pDeviceName, pUserName, pUserPassword,
						pOpenInfo, hOpenChannel);
				if (openchannel != 0) {
					System.out.println("openchannel=" + openchannel);
				}
				System.out.println("hOpenChannel=" + hOpenChannel.getValue());
				if (openchannel == 0) {
					start.setEnabled(false);
					stop.setEnabled(true);
				}
			}
		});
		stop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int closeOk = HHNet.INSTANCE.HHNET_CloseChannel(hOpenChannel
						.getValue());
				if (closeOk == 0) {
					// SwingUtilities.invokeLater(new Runnable() {
					//
					// @Override
					// public void run() {
					// pvideo.invalidate();
					// pvideo.repaint();
					// }
					// });
					//
					start.setEnabled(true);
					stop.setEnabled(false);
				} else {
					System.out.println("closeOk:" + closeOk);
				}
			}
		});
		final HHOPEN_PICTURE_INFO picOpen = new HHOPEN_PICTURE_INFO();
		picOpen.dwClientID = dwClientID;
		picOpen.nOpenChannel = nOpenChannel;
		picOpen.protocolType = protocolType;
		picOpen.pCallbackContext = videoWnd;
		picOpen.funcPictureCallback = new PictureCallback() {

			@Override
			public void invoke(int hPictureChn, Pointer pPicData, int nPicLen,
					long dwClientID, HWND pContext) {
				System.out.println("funcPictureCallback:");
				// ImageIcon image = new ImageIcon(pPicData.getByteArray(0,
				// nPicLen));
				// BufferedImage buffImg = new BufferedImage(image.getImage()
				// .getWidth(null), image.getImage().getHeight(null),
				// BufferedImage.TYPE_INT_RGB);
				// Graphics2D g = buffImg.createGraphics();
				// g.drawImage(buffImg, 0, 0, image.getImageObserver());
				// g.dispose();
				// try {
				// ImageIO.write(buffImg, "JPG", new File("d:\\a.jpg"));
				// } catch (IOException e) {
				// e.printStackTrace();
				// }
			}
		};
		final IntByReference hOpenPicture = new IntByReference();

		capture.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (hOpenPicture.getValue() > 0) {
					HHNet.INSTANCE.HHNET_ClosePicture(hOpenPicture.getValue());
				}
				int openPicOk = HHNet.INSTANCE.HHNET_OpenPicture(pServerIP,
						wServerPort, pDeviceName, pUserName, pUserPassword,
						picOpen, hOpenPicture);
				System.out.println("openPicOk:" + openPicOk);
				if (openPicOk == 0) {
					int hhnet_CapturePicture = HHNet.INSTANCE
							.HHNET_CapturePicture(hOpenPicture.getValue());
					System.out.println("capture:" + hhnet_CapturePicture);
				}
			}
		});

	}
}
